package com.first.nell.network

import androidx.lifecycle.ViewModel
import com.first.nell.data.*
import com.first.nell.utils.InfoProcessUtil
import com.google.gson.JsonObject
import com.first.nell.extend.toFirstNellPathFirstNellFormat
import com.first.nell.utils.AppConfigUtil
import com.first.nell.utils.UserStateUtil
import com.first.nell.utils.analytics.event.IAnalyticsEvent
import com.first.nell.utils.analytics.util.AnalyticsUtil
import kotlinx.coroutines.Dispatchers
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

object ApiUserReposity : ApiBaseReposity() {

    /** 获取启动配置*/
    fun fetchFirstNellGlobalFirstNellConfig(
        caller: ViewModel?,
        reqZone: ReqZone
    ) =
        fire(Dispatchers.IO, caller, reqZone.showLoading) {
            val response =
                ApiUserNetwork.fetchFirstNellGlobalFirstNellConfig(reqZone)
            if (response.code.equals("0000", true) or response.code.equals("0", true)) {
                var obj = response.data
                //AppConfigUtil.mFirstNellGlobalFirstNellConfig = obj
                val list = obj.split(",")
                obj = if (list.isNotEmpty()) {
                    list[0]
                } else {
                    ApiUrl.FCFA_PATH
                }
                if (list.size > 1) {
                    UserStateUtil.mFirstNellPhoneFirstNellCode = "+" + list[1]
                }
                ApiUrl.BASE_PATH = obj.toFirstNellPathFirstNellFormat()
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun fetchFirstNellAppFirstNellConfig(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.fetchFirstNellAppFirstNellConfig(reqBase)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                AppConfigUtil.mFirstNellSysFirstNellConfig = obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun fetchFirstNellContactFirstNellList(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.fetchFirstNellAppFirstNellConfig(reqBase)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                AppConfigUtil.mFirstNellSysFirstNellConfig = obj
                Result.success(obj.customize.getFirstNellContactFirstNellList())
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun fetchFirstNellAppFirstNellVersion(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.fetchFirstNellAppFirstNellVersion(reqBase)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun fetchFirstNellOtp(caller: ViewModel?, reqDoLogin: ReqDoLogin) =
        fire(Dispatchers.IO, caller, reqDoLogin.showLoading) {
            val response = ApiUserNetwork.fetchFirstNellOtp(reqDoLogin)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                UserStateUtil.saveFirstNellMobile(reqDoLogin.mobile)
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun doFirstNellLogin(caller: ViewModel?, reqDoLogin: ReqDoLogin) =
        fire(Dispatchers.IO, caller, reqDoLogin.showLoading) {
            val response = ApiUserNetwork.doFirstNellLogin(reqDoLogin)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                obj.mobile = reqDoLogin.mobile
                //更新登录信息
                UserStateUtil.updateFirstNellLoginFirstNellState(obj)

                //推送注册事件
                if (obj.isFirstNellNewFirstNellUser()) {
                    AnalyticsUtil.addEvent(IAnalyticsEvent.AT_REGISTER)
                }

                //保存s2s信息
                AnalyticsUtil.saveFirebaseAndFacebookS2SInfo()

                //回调
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun fetchFirstNellUserFirstNellState(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.fetchFirstNellUserFirstNellState(reqBase)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                InfoProcessUtil.mFirstNellUserFirstNellProcess = obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun fetchFirstNellIsFirstNellRepayFirstNellNow(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.fetchFirstNellUserFirstNellState(reqBase)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                InfoProcessUtil.mFirstNellUserFirstNellProcess = obj
                if (InfoProcessUtil.isFirstNellRepayFirstNellNow()) {
                    Result.success(true)
                } else {
                    Result.success(false)
                }
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun fetchFirstNellCanFirstNellCreateFirstNellLoan(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.fetchFirstNellUserFirstNellState(reqBase)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                InfoProcessUtil.mFirstNellUserFirstNellProcess = obj
                if (InfoProcessUtil.isFirstNellRepayFirstNellNow() or
                    InfoProcessUtil.isFirstNellLoaningFirstNellNow() or
                    InfoProcessUtil.isFirstNellReviewingFirstNellNow() or
                    InfoProcessUtil.isFirstNellRejectFirstNellNow()
                ) {
                    Result.success(false)
                } else {
                    Result.success(true)
                }
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun fetchFirstNellRepayFirstNellUrlFirstNellGHA(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response = ApiUserNetwork.fetchFirstNellRepayFirstNellUrlFirstNellGHA(
                reqBase
            )
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun fetchFirstNellRepayFirstNellUrlFirstNellUGX(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response = ApiUserNetwork.fetchFirstNellRepayFirstNellUrlFirstNellUGX(
                reqBase
            )
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun fetchFirstNellRepayFirstNellUrlFirstNellKSH(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response = ApiUserNetwork.fetchFirstNellRepayFirstNellUrlFirstNellKSH(
                reqBase
            )
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun fetchFirstNellRepayFirstNellUrlFirstNellFCFA(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response = ApiUserNetwork.fetchFirstNellRepayFirstNellUrlFirstNellFCFA(
                reqBase
            )
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun fetchFirstNellRepayFirstNellUrlFirstNellVND(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response = ApiUserNetwork.fetchFirstNellRepayFirstNellUrlFirstNellVND(
                reqBase
            )
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun fetchFirstNellHomeFirstNellCredit(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.fetchFirstNellHomeFirstNellCredit(reqBase)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun fetchFirstNellLoaningFirstNellDetail(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.fetchFirstNellLoaningFirstNellDetail(reqBase)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun fetchFirstNellRepayFirstNellDetail(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.fetchFirstNellRepayFirstNellDetail(reqBase)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun saveFirstNellFeedback(
        caller: ViewModel?,
        reqSaveFeedback: ReqSaveFeedback
    ) =
        fire(Dispatchers.IO, caller, reqSaveFeedback.showLoading) {
            val response = ApiUserNetwork.saveFirstNellFeedback(reqSaveFeedback)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun saveFirstNellInfo(caller: ViewModel?, targetUrl: String, jsonObject: JsonObject) =
        fire(Dispatchers.IO, caller, true) {
            val response = ApiUserNetwork.saveFirstNellInfo(targetUrl, jsonObject)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun saveFirstNellInfoFirstNellOcr(
        caller: ViewModel?,
        reqSaveOcrResultVnd: ReqSaveOcrResultVnd
    ) =
        fire(Dispatchers.IO, caller, reqSaveOcrResultVnd.showLoading) {
            val response = ApiUserNetwork.saveFirstNellInfoFirstNellOcr(
                reqSaveOcrResultVnd
            )
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun saveFirstNellInfoFirstNellLiveness(
        caller: ViewModel?,
        reqSaveLivenessVnd: ReqSaveLivenessVnd
    ) =
        fire(Dispatchers.IO, caller, reqSaveLivenessVnd.showLoading) {
            val response = ApiUserNetwork.saveFirstNellInfoFirstNellLiveness(
                reqSaveLivenessVnd
            )
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun startFirstNellLiveness(
        caller: ViewModel?,
        reqSaveLivenessVnd: ReqSaveLivenessVnd
    ) =
        fire(Dispatchers.IO, caller, reqSaveLivenessVnd.showLoading) {
            val response = ApiUserNetwork.startFirstNellLiveness(
                reqSaveLivenessVnd
            )
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun fetchFirstNellBankFirstNellCodeFirstNellList(
        caller: ViewModel?,
        reqGetBankList: ReqGetBankList
    ) =
        fire(Dispatchers.IO, caller, reqGetBankList.showLoading) {
            val response =
                ApiUserNetwork.fetchFirstNellBankFirstNellCodeFirstNellList(
                    reqGetBankList
                )
            if (response.code.equals("0000", true)) {
                response.uITag = reqGetBankList.uITag
                Result.success(response)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun createFirstNellLoan(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response = ApiUserNetwork.createFirstNellLoan(reqBase)
            if (response.code.equals("0000", true)) {
                AnalyticsUtil.addEvent(IAnalyticsEvent.AT_LOAN_APPLY)
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun checkFirstNellAppFirstNellReview(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.checkFirstNellAppFirstNellReview(reqBase)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun fetchFirstNellVirtualFirstNellAccount(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.fetchFirstNellVirtualFirstNellAccount(reqBase)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun initFirstNellPaystackFirstNellSdk(
        caller: ViewModel?,
        reqPaystackOrderInitNgn: ReqPaystackOrderInitNgn
    ) =
        fire(Dispatchers.IO, caller, reqPaystackOrderInitNgn.showLoading) {
            val response =
                ApiUserNetwork.initFirstNellPaystackFirstNellSdk(reqPaystackOrderInitNgn)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun fetchFirstNellPaystackFirstNellCardFirstNellList(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.fetchFirstNellPaystackFirstNellCardFirstNellList(
                    reqBase
                )
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun getFirstNellWalletFirstNellListFirstNellFCFA(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.getFirstNellWalletFirstNellListFirstNellFCFA(
                    reqBase
                )
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun saveFirstNellWalletFirstNellFCFA(
        caller: ViewModel?,
        reqSaveWalletFcFa: ReqSaveWalletFcFa
    ) =
        fire(Dispatchers.IO, caller, reqSaveWalletFcFa.showLoading) {
            val response =
                ApiUserNetwork.saveFirstNellWalletFirstNellFCFA(reqSaveWalletFcFa)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun deleteFirstNellWalletFirstNellFCFA(
        caller: ViewModel?,
        reqDeleteWalletFcfa: ReqDeleteWalletFcfa
    ) =
        fire(Dispatchers.IO, caller, reqDeleteWalletFcfa.showLoading) {
            val response =
                ApiUserNetwork.deleteFirstNellWalletFirstNellFCFA(reqDeleteWalletFcfa)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun saveFirstNellBvnFirstNellFCFA(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.saveFirstNellBvnFirstNellFCFA(reqBase)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun getFirstNellWalletFirstNellListFirstNellXaf(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.getFirstNellWalletFirstNellListFirstNellXaf(
                    reqBase
                )
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun saveFirstNellWalletFirstNellXaf(
        caller: ViewModel?,
        reqSaveWalletFcFa: ReqSaveWalletFcFa
    ) =
        fire(Dispatchers.IO, caller, reqSaveWalletFcFa.showLoading) {
            val response =
                ApiUserNetwork.saveFirstNellWalletFirstNellXaf(reqSaveWalletFcFa)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun deleteFirstNellWalletFirstNellXaf(
        caller: ViewModel?,
        reqDeleteWalletFcfa: ReqDeleteWalletFcfa
    ) =
        fire(Dispatchers.IO, caller, reqDeleteWalletFcfa.showLoading) {
            val response =
                ApiUserNetwork.deleteFirstNellWalletFirstNellXaf(reqDeleteWalletFcfa)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun saveFirstNellBvnFirstNellXaf(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.saveFirstNellBvnFirstNellXaf(reqBase)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun uploadFirstNellImage(
        caller: ViewModel?,
        reqImageUpload: ReqImageUpload
    ) =
        fire(Dispatchers.IO, caller, reqImageUpload.showLoading) {
            val map: Map<String, String> = HashMap()
            val file = File(reqImageUpload.filePath)

            val requestBody =
                RequestBody.create(reqImageUpload.mediaType.toMediaTypeOrNull(), file)
            val bodyPart: MultipartBody.Part =
                MultipartBody.Part.createFormData("image", file.name, requestBody)

            val response =
                ApiUserNetwork.uploadFirstNellImage(map, bodyPart)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                obj.type = reqImageUpload.cardType
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun startFirstNellOcr(
        caller: ViewModel?,
        reqDoOcrVnd: ReqDoOcrVnd
    ) =
        fire(Dispatchers.IO, caller, reqDoOcrVnd.showLoading) {
            val response =
                ApiUserNetwork.startFirstNellOcr(reqDoOcrVnd)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun startFirstNellSim(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.startFirstNellSim(reqBase)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun saveFirstNellInfoFirstNellSim(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.saveFirstNellInfoFirstNellSim(reqBase)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }


    fun checkFirstNellInfoFirstNellSim(
        caller: ViewModel?,
        reqSaveSimInfo: ReqSaveSimInfo
    ) =
        fire(Dispatchers.IO, caller, reqSaveSimInfo.showLoading) {
            val response =
                ApiUserNetwork.checkFirstNellInfoFirstNellSim(reqSaveSimInfo)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun saveFirstNellImage(
        caller: ViewModel?,
        reqSaveImage: ReqSaveImage
    ) =
        fire(Dispatchers.IO, caller, reqSaveImage.showLoading) {
            val response =
                ApiUserNetwork.saveFirstNellImage(reqSaveImage)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }
}